package com.zzy.sqlSession;

import com.zzy.pojo.Configuration;
import com.zzy.pojo.MappedStatement;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件描述: <br>
 * 版权所有: Copyright (c) 2021/5/1<br>
 * 完成日期: 2021/5/1<br>
 *
 * @author zainzhang
 */
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    private SimpleExecutor simpleExecutor = new SimpleExecutor();

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> list = selectList(statementId, params);
        if (list.size() == 1) {
            return (T) list.get(0);
        } else if (list.size() > 1) {
            throw new RuntimeException("查询结果过多");
        }
        return null;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMapperStatementMap().get(statementId);
        return simpleExecutor.query(configuration, mappedStatement, params);
    }

    @Override
    public int insert(String statementId, Object... params) throws Exception {
        return update(statementId, params);
    }

    @Override
    public int update(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMapperStatementMap().get(statementId);
        return simpleExecutor.update(configuration, mappedStatement, params);
    }

    @Override
    public int delete(String statementId, Object... params) throws Exception {
        return update(statementId, params);
    }


    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{mapperClass}, (proxy, method, objects) -> {
            // proxy当前代理对象的引用，method 调用的方法，objects传递的参数
            // 执行jdbc
            String name = method.getName();
            String calssMethod = method.getDeclaringClass().getName();
            String statementid = calssMethod + "." + name;

            MappedStatement statement = configuration.getMapperStatementMap().get(statementid);
            switch (statement.getSqlCommandType()) {
                case SELECT:
                    // 获取方法返回值类型
                    Type genericReturnType = method.getGenericReturnType();
                    // 判断是否使用了泛型类型参数化
                    if (genericReturnType instanceof ParameterizedType) {
                        return selectList(statementid, objects);
                    }
                    return selectOne(statementid, objects);
                case INSERT:
                    return insert(statementid, objects);
                case UPDATE:
                    return update(statementid, objects);
                case DELETE:
                    return delete(statementid, objects);
                default:
                    throw new RuntimeException("没有该类型的语句");
            }
        });
        return (T)proxyInstance;
    }

    @Override
    public void close() throws SQLException {
        simpleExecutor.close();
    }
}
